//
//  MessageNotifyViewController.m
//  MIDI
//
//  Created by E.M.Game on 15/9/24.
//  Copyright (c) 2015年 QXSX. All rights reserved.
//

#import "MessageNotifyViewController.h"
#import "UIViewController+NavItem.h"
#import "AppConfig.h"
#import <AudioToolbox/AudioToolbox.h>
#import "AccountTool.h"
#import "GeTuiSdk.h"

@interface MessageNotifyViewController ()
@property (weak, nonatomic) IBOutlet UISwitch *messageSwitchBtn;//消息提醒开关

@property (weak, nonatomic) IBOutlet UISwitch *voiceSwitchBtn;//声音开关
@property (weak, nonatomic) IBOutlet UISwitch *zhenDongSwitchBtn;//震动开关
@end

@implementation MessageNotifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.messageSwitchBtn.tag = 1;
    self.voiceSwitchBtn.tag = 2;
    self.zhenDongSwitchBtn.tag = 3;
    [self.messageSwitchBtn addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    [self.voiceSwitchBtn addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    [self.zhenDongSwitchBtn addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    
    if ([[AccountTool takeMessageWarn] isEqualToString:On])
    {
        [self.messageSwitchBtn setOn:YES];
    }
    else
    {
        [self.messageSwitchBtn setOn:NO];
    }
    
    if ([[AccountTool takeSystemSound] isEqualToString:On])
    {
        [self.voiceSwitchBtn setOn:YES];
    }
    else
    {
        [self.voiceSwitchBtn setOn:NO];
    }
    
    if ([[AccountTool takeVibration] isEqualToString:On])
    {
        [self.zhenDongSwitchBtn setOn:YES];
    }
    else
    {
        [self.zhenDongSwitchBtn setOn:NO];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configNavigationItem];
}

-(void) configNavigationItem
{
    [self.tabBarController configNavigationBarTintColor:UIColorRGBA(0, 0, 0, 1)];
    [self.navigationController.navigationBar setBarTintColor:MIDIColorOfNavigationBar];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationItem.title = @"消息提醒";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],UITextAttributeFont,nil]];
    
    [self configNavigationBackAction:^{
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
}

- (void)switchAction : (UISwitch *)sender
{
    
    if([sender isOn])
    {
        if(sender.tag == 1)
        {
            [GeTuiSdk setPushModeForOff:NO];
            [AccountTool saveMessageWarn:On];
        }
        else if(sender.tag == 2)
        {
            AudioServicesPlaySystemSound(1007);
            
            [AccountTool saveSystemSound:On];
        }
        else if(sender.tag == 3)
        {
            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
            
            [AccountTool saveVibration:On];
        }
    }
    else
    {
        if(sender.tag == 1)
        {
            [GeTuiSdk setPushModeForOff:YES];
            
            [AccountTool saveMessageWarn:Off];
        }
        else if(sender.tag == 2)
        {
            AudioServicesDisposeSystemSoundID(1007);

            [AccountTool saveSystemSound:Off];
        }
        else if(sender.tag == 3)
        {
            AudioServicesDisposeSystemSoundID(kSystemSoundID_Vibrate);

            [AccountTool saveVibration:Off];
        }
    }
}

@end




//#import "LxxPlaySound.h"
//@implementation LxxPlaySound
//
//-(id)initForPlayingVibrate
//{  self = [super init];
//     if (self) {
//      soundID = kSystemSoundID_Vibrate;
//    }
//    return self;
//}
//
//-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type
//{ self = [super init];
//    if (self)
//    { NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:resourceName ofType:type]; 38  39
//        if (path)
//        {
//            SystemSoundID theSoundID;
//            OSStatus error =AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &theSoundID);
//            if (error == kAudioServicesNoError)
//            {
//                soundID = theSoundID;
//            }
//            else
//            {
//                NSLog(@"Failed to create sound ");
//            }
//        }
//    }
//    return self; 62  63
//} 64  65 66  67
//-(id)initForPlayingSoundEffectWith:(NSString *)filename 68  69
//{ 70  71 self = [super init]; 72  73
//    if (self)
//    { 74  75 NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil]; 76  77
//        if (fileURL != nil) 78  79
//        { 80  81 SystemSoundID theSoundID; 82  83
//            OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID); 84  85
//            if (error == kAudioServicesNoError)
//            { 86  87 soundID = theSoundID; 88  89 }
//            else
//            {NSLog(@"Failed to create sound "); 92  93
//            }
//        }
//    }
//    return self;
//}
//
//-(void)play106
//{
//    AudioServicesPlaySystemSound(soundID);
//}
//
//-(void)dealloc116
//{
//    AudioServicesDisposeSystemSoundID(soundID);120 121
//}
//@end

















